<template>
  <div class="table-box">
    <pro-table ref="proTableRef" row-key="id" :columns="columns" :request-api="getSequencePageApi">
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button v-auth="'sys:sequence:add'" type="primary" :icon="CirclePlus" @click="handleAdd"> 新增序列 </el-button>
      </template>
      <!-- 分隔符 -->
      <template #delimiter="{ row }">
        {{ row.delimiter || "--" }}
      </template>
      <!-- 当前编码 -->
      <template #currentCode="{ row }">
        {{ row.currentCode || "--" }}
      </template>
      <!-- 当前重置依赖 -->
      <template #currentReset="{ row }">
        {{ row.currentReset || "--" }}
      </template>
      <!-- 状态 -->
      <template #isEnable="scope">
        <el-switch
          v-model="scope.row.isEnable"
          :loading="scope.row.loading"
          active-text="已启用"
          inactive-text="已禁用"
          inline-prompt
          :before-change="handleEnable.bind(scope, scope.row)"
        />
      </template>
      <!-- 菜单操作 -->
      <template #operation="{ row }">
        <el-button type="primary" link :icon="Memo" @click="handleShowRule(row)"> 规则 </el-button>
        <el-button v-auth="'sys:sequence:edit'" type="primary" link :icon="EditPen" @click="handleEdit(row)"> 编辑 </el-button>
        <el-button v-auth="'sys:sequence:del'" type="danger" link :icon="Delete" @click="handleDelete(row)"> 删除 </el-button>
      </template>
    </pro-table>
    <!-- 编辑序列 -->
    <sequence-edit ref="editDialogRef" />
    <!-- 序列规则 -->
    <sequence-rule ref="ruleDrawerRef" />
  </div>
</template>

<script setup lang="ts" name="sequenceManage">
import type { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import type { SysSequence } from "@/api/interface";
import { ref } from "vue";
import { useHandleData } from "@/hooks/useHandleData";
import { statusConst, sequenceResetConst } from "@/utils/dict";
import {
  addSequenceApi,
  editSequenceApi,
  switchEnableSequenceApi,
  deleteSequenceApi,
  getSequencePageApi
} from "@/api/modules/system/sequence";
import { Delete, EditPen, CirclePlus, Memo } from "@element-plus/icons-vue";
import ProTable from "@/components/ProTable/index.vue";
import SequenceEdit from "./components/edit.vue";
import SequenceRule from "./components/rule.vue";

/** 表格实例 */
const proTableRef = ref<ProTableInstance>();
/** 编辑弹窗实例 */
const editDialogRef = ref<InstanceType<typeof SequenceEdit> | null>(null);
/** 序列规则实例 */
const ruleDrawerRef = ref<InstanceType<typeof SequenceRule> | null>(null);

/** 表格配置项 */
const columns: ColumnProps[] = [
  { prop: "name", label: "序列名称", width: 140, search: { el: "input" } },
  { prop: "delimiter", label: "分隔符", width: 100 },
  { prop: "sequenceReset", label: "重置规则", width: 100, tag: true, enum: sequenceResetConst },
  { prop: "step", label: "步长", width: 100 },
  { prop: "currentNo", label: "当前值", width: 100 },
  { prop: "currentCode", label: "当前编码" },
  { prop: "currentReset", label: "当前重置依赖", width: 120 },
  { prop: "description", label: "描述(说明)" },
  { prop: "isEnable", label: "状态", width: 100, enum: statusConst, search: { el: "select" } },
  { prop: "operation", label: "操作", width: 240, fixed: "right" }
];

/**
 * 添加序列
 *
 * @returns void
 */
const handleAdd = () => {
  const editParams = {
    title: "添加序列",
    sequence: {
      step: 1,
      currentNo: 0
    },
    getTableList: proTableRef.value?.getTableList,
    submitApi: addSequenceApi
  };
  editDialogRef.value?.acceptParams(editParams);
};

/**
 * 编辑序列
 *
 * @param param 选中的序列
 * @returns void
 */
const handleEdit = (param: Partial<SysSequence.Result>) => {
  const editParams = {
    title: "编辑序列",
    sequence: { ...param },
    getTableList: proTableRef.value?.getTableList,
    submitApi: editSequenceApi
  };
  editDialogRef.value?.acceptParams(editParams);
};

/**
 * 启用/禁用序列
 *
 * @param param 选中的序列
 * @returns void
 */
const handleEnable = (param: Partial<SysSequence.Result> & { loading?: boolean }) => {
  param.loading = true;
  return switchEnableSequenceApi({ id: param.id! })
    .then(({ data }) => {
      if (data) {
        return Promise.resolve(true);
      } else return Promise.reject(new Error("修改状态失败"));
    })
    .finally(() => {
      param.loading = false;
    });
};

/**
 * 显示序列规则
 *
 * @param param 选中的序列
 * @returns void
 */
const handleShowRule = (param: Partial<SysSequence.Result>) => {
  const ruleParams = {
    title: `${param.name} 序列规则`,
    sequenceId: param.id!
  };
  ruleDrawerRef.value?.acceptParams(ruleParams);
};

/**
 * 删除序列
 *
 * @param param 选中的序列
 * @returns void
 */
const handleDelete = (param: Partial<SysSequence.Result>) => {
  useHandleData(deleteSequenceApi, { ids: [param.id] }, `删除【${param.name}】序列`).then(res => {
    if (res) proTableRef.value?.getTableList();
  });
};
</script>
